<template>
	<view v-if="showIndex">
		<scroll-view :scroll-top="0" :scroll-y="true" class="scroll-Y">
			<uni-notice-bar show-close color="#ffffff" background-color="#cb0d0d" :speed="50" scrollable show-icon
				single text="SMALL商城仅提供练习不投入实际使用!" />
			<view class="content">
				<view class="swiper">
					<view class="uni-margin-wrap">
						<swiper class="swiper" circular :autoplay="true" :indicator-dots="true" :interval="4000"
							:duration="500">
							<swiper-item v-for="(item,index) in swipeList" :key="index">
								<view class="swiper-item uni-bg-red">
									<image :src="item.pic" mode="aspectFill" style="width: 100%;"></image>
								</view>
							</swiper-item>
						</swiper>
					</view>
				</view>

				<view class="nav">
					<uni-grid :column="4" :highlight="true" :showBorder="false" :square="false">
						<uni-grid-item>
							<view @tap="gotoAllGoods">
								<view class="icon">
									<text class="iconfont icon-shangpin2"></text>
								</view>
								<text class="text">全部商品</text>
							</view>
						</uni-grid-item>

						<uni-grid-item>
							<view @tap="gotoDiscounts">
								<view class="icon" >
									<text class="iconfont icon-youhuijuan"></text>
								</view>
								<text class="text">优惠劵</text>
							</view>
						</uni-grid-item>

						<uni-grid-item>
							<view @tap="topic">
								<view class="icon" >
									<text class="iconfont icon-line-magicmofabang"></text>
								</view>
								<text class="text">话题</text>
							</view>
						</uni-grid-item>

						<uni-grid-item>
							
						<view @tap="gotoAboutMy">
							<view class="icon" >
								<text class="iconfont icon-lianxiwomen"></text>
							</view>
							<text class="text">联系我们</text>
						</view>
						</uni-grid-item>
						
					</uni-grid>
				</view>


				<view class="brand">
					<uni-section title="品  牌  推  荐">
						<uni-grid :column="4" :highlight="false" :showBorder="false">
							<uni-grid-item v-for="(item, index) in brandList" :index="index" :key="item.id">
								<view class="grid-item-box" style="background-color: #fff;"
									@tap="gotoBrand(item.id,item.name)">
									<view class="brandImage">
										<image :src="item.logo" mode="aspectFit"></image>
									</view>
									<text class="text">{{item.name}}</text>
								</view>
							</uni-grid-item>
						</uni-grid>
					</uni-section>
				</view>

				<!-- 新 品 商 品 -->
				<view>
					<view class="shopping">
						<text class="shoppingTitle" space="ensp">新 品 商 品</text>
					</view>
					<view class="goods">
						<view class="goodsItem" v-for="(item,index) in newProductList" :key="index"
							@tap="gotoDetail(item.id)">
							<view>
								<image :src="item.pic"></image>
							</view>
							<view class="price">
								<text class="newPrice">￥{{item.price}}</text>
								<text class="olbPrice">￥{{item.originalPrice}}</text>
							</view>
							<view>
								<text class="productTitle">
									{{item.name}}
								</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 人气推荐 -->
				<view>
					<view class="recommendshopping">
						<text class="shoppingTitle" space="ensp">人 气 推 荐</text>
					</view>
					<view class="goods">
						<view class="goodsItem" v-for="(item,index) in recommendProductList" @tap="gotoDetail(item.id)"
							:key="index">
							<view>
								<image :src="item.pic"></image>
							</view>
							<view class="price">
								<text class="newPrice">￥{{item.price}}</text>
								<text class="olbPrice">￥{{item.originalPrice}}</text>
							</view>
							<view>
								<text class="productTitle">
									{{item.name}}
								</text>
							</view>
						</view>
					</view>
				</view>

				<view class="bottom">
					没有更多数据了哦~
				</view>
				<!-- 返回頂部  -->
				<view class="ScrollTop" v-if="scrollTop" @click="tapscroll">
				</view>
			</view>
		</scroll-view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				swipeList: [],
				brandList: [],
				newProductList: [],
				recommendProductList: [],
				scrollTop: false,
				showIndex:false
			}
		},
		onLoad() {
			this.getHomeList()
		},
		onHide() {
			this.scrollTop = false
		},
		// 监听下拉
		onPullDownRefresh(success, fail) {
			this.getHomeList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 800)
		},
		methods: {
			// 获取首页数据
			async getHomeList() {
				let res = await this.$api.home.getHomeList()
				if (res.code == 200) {
					// 轮播数据
					this.swipeList = res.data.advertiseList
					// 品牌数据
					this.brandList = res.data.brandList
					// 新品数据
					this.newProductList = res.data.newProductList
					// 人气推荐
					this.recommendProductList = res.data.recommendProductList
					
					this.showIndex=true
				}
			},
			tapscroll() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				})
			},
			// 去往全部商品
			gotoAllGoods() {
				uni.navigateTo({
					url: "/pages/view/allGoods"
				})
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: `/pages/view/ProductDetail?Pid=${id}`
				})
			},
			gotoBrand(id, name) {
				uni.navigateTo({
					url: `/pages/view/brandList?bid=${id}&brandName=${name}`
				})
			},
			gotoDiscounts() {
				if (uni.getStorageSync("TOKEN")) {
					uni.navigateTo({
						url: '/pages/view/Discounts'
					})
				} else {
					uni.showModal({
						title: "您还没登录呐",
						confirmText: "去登录",
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/view/Login'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})

				}
			},
			topic() {
				uni.showToast({
					title: "敬请期待！",
					icon: "loading"
				})
			},
			gotoAboutMy(){
				uni.navigateTo({
					url:"/pages/view/AboutMy"
				})
			}
		},
		onPageScroll(e) {
			if (e.scrollTop >= 500) {
				this.scrollTop = true
			} else {
				this.scrollTop = false
			}
		},


	}
</script>

<style lang="scss" scoped>
	.content {
		margin: 0 5rpx;
		background-color: #e8e8e8;

		/deep/ .uni-noticebar {
			margin: 0;
		}

		.uni-margin-wrap {
			width: 690rpx;
			width: 100%;
			border-top-left-radius: 10rpx;
			border-top-right-radius: 10rpx;
			overflow: hidden;
		}

		.swiper {
			height: 400rpx;
		}

		.swiper-item {
			display: block;
			height: 300rpx;
			line-height: 300rpx;
			text-align: center;
		}

		.swiper-list {
			margin-top: 40rpx;
			margin-bottom: 0;
		}

		.nav {
			text-align: center;
			background-color: white;
			border-radius: 10rpx;
			overflow: hidden;

			.icon {
				height: 120rpx;
				width: 120rpx;
				border-radius: 100rpx;
				margin: 10rpx auto;
				background-color: #cb0d0d;

				text {
					line-height: 120rpx;
					font-size: 50rpx;
					color: white;
				}
			}
		}

		.brand {
			background-color: white;
			margin-top: 8rpx;
			text-align: center;
			border-radius: 10rpx;
			overflow: hidden;

			/deep/ .uni-section-header {
				border-bottom: 1px solid gainsboro;
				padding: 7px 10px !important;
			}

			/deep/ .uni-section__content-title {
				span {
					font-weight: 700;
					color: #cb0d0d;
					font-size: 19px !important;
				}
			}

			.brandImage {
				width: 90rpx;
				height: 90rpx;
				margin: 10rpx auto;
				border-radius: 90rpx;
				overflow: hidden;

				image {
					width: 90rpx;
					height: 90rpx;
				}
			}
		}

		/deep/ .uni-section {
			margin-top: 6px;
		}

		.shopping {
			height: 100rpx;
			width: 100%;
			border: 1px solid #e5e5e5;
			text-align: center;
			box-shadow: 0 0 8px #efefef;
			background-color: white;
			margin-top: 8rpx;

			.shoppingTitle {
				line-height: 100rpx;
				font-size: 40rpx;
				color: #cb0d0d;
				font-weight: 700;
			}
		}

		// 人气推荐
		.recommendshopping {
			height: 100rpx;
			width: 100%;
			text-align: center;
			box-shadow: 0 0 8px #efefef;
			background-color: white;
			margin-top: 18rpx;

			.shoppingTitle {
				line-height: 100rpx;
				font-size: 40rpx;
				color: #cb0d0d;
				font-weight: 700;
			}
		}

		.goods {
			background-color: #eeeeee;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.goodsItem {
				width: 355rpx;
				margin: 8rpx 8rpx;
				background-color: #ffffff;
				border-radius: 10rpx;
				overflow: hidden;
				box-shadow: 0px 0px 5px #e2e2e2;

				image {
					width: 100%;
					height: 200px;
				}

				.price {
					padding-left: 10rpx;

					.newPrice {
						font-size: 45rpx;
						color: #cb0d0d;
					}

					.olbPrice {
						color: gray;
						text-decoration: line-through;
					}

					text:nth-child(2) {
						margin-left: 17rpx;
					}
				}

				.shoppingsubTltie {
					padding-bottom: 30rpx;
				}

				.productTitle {
					color: gray;
				}
			}
		}

		.bottom {
			width: 100%;
			padding: 20rpx;
			text-align: center;
			color: gray;
		}

		.ScrollTop {
			height: 80rpx;
			width: 80rpx;
			position: fixed;
			bottom: 140rpx;
			right: 45rpx;
			z-index: 1000;
			background: white;
			border-radius: 80rpx;
			background-image: url("@/static/sroTop.png");
			background-size: 100% 100%;
		}
	}
</style>
